home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7229 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  62 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.eunet.fi!nullnet!ichaos!jlaiho
  3. From: jlaiho@ichaos.nullnet.fi (Juha Laiho)
  4. Subject: Function pointers; example code 
  5. Content-Type: text/plain; charset=ISO-8859-1
  6. Content-Transfer-Encoding: 8bit
  7. Organization: NullNet r.y. 
  8. Message-ID: <DMwzBL.HDt@ichaos.nullnet.fi>
  9. Mime-Version: 1.0
  10. Date: Sat, 17 Feb 1996 09:50:09 GMT
  11.  
  12. Here's a short program I wrote as an example of function pointers. I'd
  13. appreciate _any_ comments about it.
  14.  
  15. ---SNIP---
  16. #include <stdio.h>
  17.  
  18. typedef void f(int); /* f is a type that is a function taking one int
  19.                       * argument and returning an int
  20.                       */
  21. typedef f *ptf;      /* ptf is a type that is a pointer to a function */
  22. typedef ptf aptf[];  /* aptf is a type that is an array of ptfs */
  23.  
  24. f a, b, c; /* Define three functions a, b and c; each is 'void f(int)' */
  25.  
  26. int
  27. main ()
  28. {
  29.     aptf fa={ a, b, c }; /* Declare and initialise an array of function
  30.                           * pointers
  31.                           */
  32.     int i;
  33.  
  34.     for(i=0; i<3; i++)
  35.         fa[i](i);
  36.     return 0;
  37. }
  38.  
  39. void
  40. a (int x)
  41. {
  42.     printf("a: %d\n",x);
  43. }
  44.  
  45. void
  46. b (int y)
  47. {
  48.     printf("b: %d\n",y);
  49. }
  50.  
  51. void
  52. c (int z)
  53. {
  54.     printf("c: %d\n",z);
  55. }
  56. ---SNIP---
  57. -- 
  58. Wolf  a.k.a.  Juha Laiho     Espoo, Finland
  59. (GEEK CODE 3.0) GIT d- s+: a- C++ UH++++$ UL++++ P- L+++ E--- W+ N+++ !K w !O
  60.             !M V PS(+) PE Y+ !PGP t- 5? X? R tv- b+ DI? D+ G e+ h!>--- r++ y+
  61. "...cancel my subscription to the resurrection!" (Jim Morrison)
  62.